﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
using SmartXLS;
using System.Drawing;
using UygulamaTakbisService.wsTakbisServis;

namespace UygulamaTakbisService
{
    public class GercekKisiYaz
    {
        public byte[] Yaz(List<GercekKisiBilgi> gercekKisiBilgiList, string serverKayitYolu)
        {
            WorkBook workBook = new WorkBook();
            workBook.setSheetName(0, "GercekKisi Listesi");

            int satir = 0;
            int sutun = 0;

            workBook.setText(satir, sutun, "Gerçek Kisi ID");
            sutun++;
            workBook.setText(satir, sutun, "TCKimlikNo");
            sutun++;
            workBook.setText(satir, sutun, "Adı");
            sutun++;
            workBook.setText(satir, sutun, "Soyadı");
            sutun++;
            workBook.setText(satir, sutun, "Baba Adı");
            sutun++;
            workBook.setText(satir, sutun, "Ana Adı");
            sutun++;
            workBook.setText(satir, sutun, "Dogum Tarihi");
            sutun++;
            workBook.setText(satir, sutun, "Dogum Yeri");
            sutun++;
            workBook.setText(satir, sutun, "Cinsiyeti");
            sutun++;
            workBook.setText(satir, sutun, "Ölüm Tarihi");
            sutun++;
            workBook.setText(satir, sutun, "Aktif Mi");
            sutun++;
            RangeStyle rangeStyleBaslik = workBook.getRangeStyle(0, 0, 0, sutun);
            rangeStyleBaslik.FontBold = true;
            rangeStyleBaslik.FontColor = Color.DarkRed.ToArgb();
            rangeStyleBaslik.HorizontalAlignment = RangeStyle.HorizontalAlignmentCenter;
            rangeStyleBaslik.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
            rangeStyleBaslik.LeftBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.RightBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.TopBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.BottomBorder = RangeStyle.BorderThin;
            rangeStyleBaslik.VerticalInsideBorder = RangeStyle.BorderThin;
            workBook.setRangeStyle(rangeStyleBaslik, 0, 0, 0, sutun - 1);

            string deger = string.Empty;
            foreach (GercekKisiBilgi gercekKisiBilgi in gercekKisiBilgiList)
            {
                satir++;
                sutun = 0;

                if (gercekKisiBilgi.GercekKisiBilgiID != null)
                    deger = gercekKisiBilgi.GercekKisiBilgiID.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.TCKimlikNo != null)
                    deger = gercekKisiBilgi.TCKimlikNo.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.Ad != null)
                    deger = gercekKisiBilgi.Ad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.Soyad != null)
                    deger = gercekKisiBilgi.Soyad.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.BabaAd != null)
                    deger = gercekKisiBilgi.BabaAd.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.AnaAd != null)
                    deger = gercekKisiBilgi.AnaAd.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.DogumTarih != null)
                    deger = gercekKisiBilgi.DogumTarih.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.DogumYer != null)
                    deger = gercekKisiBilgi.DogumYer.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.Cinsiyet != null)
                    deger = gercekKisiBilgi.Cinsiyet.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.OlumTarih != null)
                    deger = gercekKisiBilgi.OlumTarih.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                if (gercekKisiBilgi.AktifMi != null)
                    deger = gercekKisiBilgi.AktifMi.ToString();
                workBook.setText(0, satir, sutun, deger);
                deger = string.Empty;
                sutun++;

                RangeStyle rangeStyleVeri = workBook.getRangeStyle(satir, 0, satir, sutun);
                rangeStyleVeri.HorizontalAlignment = RangeStyle.HorizontalAlignmentLeft;
                rangeStyleVeri.VerticalAlignment = RangeStyle.VerticalAlignmentCenter;
                rangeStyleVeri.LeftBorder = RangeStyle.BorderThin;
                rangeStyleVeri.RightBorder = RangeStyle.BorderThin;
                rangeStyleVeri.TopBorder = RangeStyle.BorderThin;
                rangeStyleVeri.BottomBorder = RangeStyle.BorderThin;
                rangeStyleVeri.VerticalInsideBorder = RangeStyle.BorderThin;
                workBook.setRangeStyle(rangeStyleVeri, satir, 0, satir, sutun - 1);
            }

            for (int i = 0; i < sutun + 1; i++)
                workBook.setColWidthAutoSize(i, true);

            workBook.write(serverKayitYolu);
            workBook.Dispose();

            GC.GetTotalMemory(false);
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.GetTotalMemory(true);

            return File.ReadAllBytes(serverKayitYolu);
        }
    }
}

